var MOD_CAE={
  DOM:new ActiveXObject("Msxml2.DOMDocument")
}

function GetCaesarFileName(dn) {
  if(GetPrefsAttr("trimaran")) {
    return GetPrefsCaesarDir()+"/"+dn+"/"+dn+".xml";
  }
  return GetPrefsCaesarDir()+dn+".xml";
}

function CaesarExportKeywords(dn) {
  if(GetPrefsAttr("trimaran")) {
  } else {
    MOD_CAE.DOM.load(GetCaesarFileName(dn));
    if(MOD_CAE.DOM.parseError.errorCode != 0) { 
      //alert(name +"not found");
      return false;
    } 
    var node=MOD_CAE.DOM.selectSingleNode("/dossier/keywords");
    if(!node) {
      var root=MOD_CAE.DOM.selectSingleNode("/dossier");
      node=MOD_CAE.DOM.createElement("keywords");
      root.appendChild(node);      
      //myDOM.selectSingleNode("/dossier/designations").append;	      
    }
    if(node.text) node.text=GetDossierKeywords();
  }
}

function CaesarExportNU(dn) {
  //alert(GetPrefsCaesarDir()+GetDossierNumber()+".xml");
  MOD_CAE.DOM.load(GetCaesarFileName(dn));
  if(MOD_CAE.DOM.parseError.errorCode != 0) { 
    //alert(name +"not found");
    return false;
  } 
  
  // NU 
  
  /*
    <subject number="1" subinvention="false">
    <subjectvalue>Processor parallele</subjectvalue> 
    <generaltext>revendications: 1-4</generaltext> 
    <generaltext>Processor parallele</generaltext> 
    <generaltext>---</generaltext> 
    <claimsvalue>1-4</claimsvalue> 
    </subject>
    </subjectlist>
  */

  // Export NU
  node=MOD_CAE.DOM.selectSingleNode("/dossier/standardsearch/partialsearch");
  //node.selectSingleNode("motivation").text="";

  if(node && GetGroupsLength()>1) {
    node.selectSingleNode("totalnumberofinventions").text=GetGroupsLength();
    var nodelist=MOD_CAE.DOM.createElement("subjectlist");    
    var grp_iter= new group_iterator();
    while(!grp_iter.end()) {
      var grpnode=node.selectSingleNode("subject[@number="+grp_iter.getValue()+"]");
      if(!grpnode) {
	grpnode=MOD_CAE.DOM.createElement("subject");
	grpnode.setAttribute("number",grp_iter.getValue());
	grpnode.setAttribute("subinvention","false");

	var grpelem=MOD_CAE.DOM.createElement("subjectvalue");
	grpelem.text=grp_iter.getTitle();
	grpnode.appendChild(grpelem);

	var grpelem=MOD_CAE.DOM.createElement("generaltext");
	if(List_count(grp_iter.getClaimList())>1) {
	  grpelem.text=efd("claims")+": "+List_tolist(grp_iter.getClaimList());
	} else {
	  grpelem.text=efd("claim")+": "+grp_iter.getClaimList();
	}
	grpnode.appendChild(grpelem);
	var grpelem=MOD_CAE.DOM.createElement("generaltext");
	grpelem.text=grp_iter.getTitle();
	grpnode.appendChild(grpelem);
	var grpelem=MOD_CAE.DOM.createElement("generaltext");
	grpelem.text="---";
	grpnode.appendChild(grpelem);
	var grpelem=MOD_CAE.DOM.createElement("claimsvalue");
	grpelem.text=List_tolist(grp_iter.getClaimList());
	grpnode.appendChild(grpelem);
	nodelist.appendChild(grpnode);
      }
      grp_iter.next();
    }
    if(!node.selectSingleNode("subjectlist")) {
      node.appendChild(nodelist); 
    } else {
      node.replaceChild(nodelist,node.selectSingleNode("subjectlist")); 
    }
  }   
  //node=MOD_CAE.DOM.selectSingleNode("/dossier/citations");
  
  /*
    <citeddocument citationsource="citedByExaminer" phase="srch" type="Patent">
  <passage> <category claimsreference="2">A</category></passage>
  <passage> <category claimsreference="" /> 
   <location type="Page">1</location> 
   <location type="Line">2</location> 
  </passage>
  <passage>
   <category claimsreference="" /> 
   <location type="Page">3</location> 
   <location type="Line">1</location> 
   <location type="ToPage">4</location> 
   <location type="ToLine">10</location> 
  </passage>
- <passage>
   <category claimsreference="" /> 
   <location type="CitedClaims">7</location> 
  </passage>
- <passage>
   <category claimsreference="" /> 
   <location type="Paragraph">33</location> 
   <location type="ToParagraph">34</location> 
  </passage>
- <passage>
   <category claimsreference="" /> 
   <location type="Paragraph">70</location> 
   </passage>
- <passage>
   <category claimsreference="" /> 
   <location type="Figure">6</location> 
  </passage>
- <passage>
  <category claimsreference="" /> 
  <location type="Page">A</location> 
  </passage>
- <passage>
  <category claimsreference="" /> 
  <location type="Page">W</location> 
  </passage>
- <passage>
  <category claimsreference="" /> 
  <location type="Column">L</location> 
  <location type="ToColumn">R</location> 
  <location type="Page">6</location> 
  <location type="Line">5</location> 
  <location type="ToLine">6</location> 
  </passage>
- <passage>
  <category claimsreference="" /> 
  </passage>
- <properties>
  <property name="examPublDate">20000321</property> 
  <property name="kindCode">A</property> 
  <property name="nonCited">false</property> 
  <property name="number">6041400</property> 
  <property name="office">US</property> 
  <property name="patentee">OZCELIK TANER [US] ET AL</property> 
  <property name="printRequest">false</property> 
  <property name="scanRequest">false</property> 
  <property name="source">EPODOC</property> 
  </properties>
  <category claimsreference="1">X</category> 
  </citeddocument>
  */

  MOD_CAE.DOM.save(GetPrefsCaesarDir()+dn+".xml");
}


function CaesarImportDossier(dn,app) {
  if(GetPrefsAttr("trimaran")) {
    if(!app) return;
    return TrimaranImportAppData(app);
  } else {
    MOD_CAE.DOM.load(GetCaesarFileName(dn));
    if(MOD_CAE.DOM.parseError.errorCode != 0) { 
      //alert(GetPrefsCaesarDir()+dn+".xml" +" not found, retrieve it with Caesar.");
      return false;
    } 
    var node=MOD_CAE.DOM.selectSingleNode("/dossier/keywords");
    if(node) {
      SetDossierKeywords(node.text);
    }
    var node=MOD_CAE.DOM.selectSingleNode("/dossier/titles/title[@valuesource=\"original\"]");
    if(node) {
      SetDossierTitle(beautytitle(node.text));
    } 
    var node=MOD_CAE.DOM.selectSingleNode("/dossier/applicants/applicant/participant/name");
    if(node) {
      SetDossierApplicant(node.text);
    }
    var node=MOD_CAE.DOM.selectSingleNode("/dossier/prioritydate");
    
    if(node && parseInt(node.text)) {
      SetDossierPri(normalizeDate(node.text));
    } else { // first filing
      var node=MOD_CAE.DOM.selectSingleNode("/dossier/filingdate");
      if(node) {
	SetDossierPri(normalizeDate(node.text));
      }
    }
  }
  if(app) {
    var retapp=[];
    var node=MOD_CAE.DOM.selectSingleNode("/dossier/id[@type=\"appl\"]");      
    if(node) {
      retapp.num=node.childNodes[0].text+node.childNodes[1].text;
      // to check 
      switch(node.childNodes[2].text) {
      case "A":
	retapp.phase=node.childNodes[0].text;
	break;
      case "W":
	retapp.phase="PCT";
	break;
      }
    } 
    var node=MOD_CAE.DOM.selectSingleNode("/dossier/filingdate");
    if(node) {
      retapp.date=normalizeDate(node.text);
    }  
    return retapp;
  }
  return "";
}     


function CaesarImportAllcitations() {
  if(GetPrefsAttr("trimaran")) {
    alert("No  support for Trimaran yet");
    return;
  }
  var allfiles=dos_dir(GetPrefsCaesarDir(),".xml");
  var progbar = new html_progbar("Refreshing doc database "+allfiles.length+" files",allfiles.length);
  for(f in allfiles) {
    progbar.message(allfiles[f]+".xml");
    CaesarImportCitations(allfiles[f],"","nosave");
    progbar.advance();
  }
  progbar.close();
  SaveDOCDB();
}


function CaesarImportCitations(dn,pn,opt) {
  if(GetPrefsAttr("trimaran")){
    if(GetClaimSetAppNum(1)) {
      MOD_CAE.DOM.load(GetCaesarFileName(GetClaimSetAppNum(1)));
    } else {
      return "";
    }
  } else {
    MOD_CAE.DOM.load(GetCaesarFileName(dn));
  }
  
  //MOD_CAE.DOM.load(GetCaesarFileName(dn));
  if(MOD_CAE.DOM.parseError.errorCode != 0) { 
    //alert(GetCaesarFileName(dn) +" not found, retrieve it with Caesar.");
    return false;
  } 

  // Inport misc info on dossier 
  var citetab=MOD_CAE.DOM.selectNodes("/dossier/citations/citeddocument");
  if(citetab) {
    for(var i=0; i < citetab.length; i++) {
      switch(citetab[i].getAttribute("type")) {
      case "Patent":
	var num=citetab[i].selectSingleNode('properties/property [@name="number"]');
	var off=citetab[i].selectSingleNode('properties/property [@name="office"]');
	var author=citetab[i].selectSingleNode('properties/property [@name="patentee"]');
	var txt=citetab[i].selectSingleNode('usercomment');
	if(off && off.text && num && num.text) {
	  var ppn=off.text+num.text;
	  if((!pn || (ppn && pn==ppn)) && txt && txt.text) {
	    SetDocDBAttr(ppn,"patentee",author.text);
	    SetDocDBRmks(ppn,dn,txt.text);
	  }
	}
	break;
      default:
	var title =citetab[i].selectSingleNode('properties/property [@name="title" ]');
	var title2=citetab[i].selectSingleNode('properties/property [@name="formattedText" ]');
	var au=citetab[i].selectSingleNode('properties/property [@name="author"]');
	var xp=citetab[i].selectSingleNode('properties/property [@name="xpNumber"]');
	var txt=citetab[i].selectSingleNode('usercomment');
	if(xp && xp.text) {
	  var xpn="XP"+xp.text;
	  if((!pn || (xpn && pn==xpn)) && txt && txt.text && au && au.text) {
	    SetDocDBAttr(xpn,"author",au.text);
	    SetDocDBRmks(xpn,dn,txt.text);
	  }	  
	}
	break;
      }
    }
  } else {
    alert("Wrong file "+dn);
  }
  if(!opt) {
    SaveDOCDB();
  }
}
